package tum0r.data.structure;

import tum0r.misc.Copy;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * 工程: JavaTools<br>
 * 包: tum0r.data.structure<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/8/21 22:22<br>
 * <br>
 */
public class PriorityQueue<T> extends Copy<PriorityQueue<T>> {
    private final LinkedList<HashMap<Integer, T>> list;

    public PriorityQueue() {
        list = new LinkedList<>();
    }

    public boolean push(int priority, T data) {
        if (priority >= 0) {
            int index = 0;
            int key;
            for (HashMap<Integer, T> integerTHashMap : list) {
                key = integerTHashMap.keySet().iterator().next();
                if (key <= priority) {
                    index = key;
                    continue;
                }
                break;
            }
            HashMap<Integer, T> temp = new HashMap<>();
            temp.put(priority, data);
            list.add(index, temp);
            return true;
        }
        return false;
    }

    public T pop() {
        if (list.size() > 0) {
            T data = list.getFirst().values().iterator().next();
            list.removeFirst();
            return data;
        }
        return null;
    }

    public boolean isEmpty() {
        return list.size() == 0;
    }

    public int size() {
        return list.size();
    }

    public T getFront() {
        return list.getFirst().values().iterator().next();
    }

    public T getBack() {
        return list.getLast().values().iterator().next();
    }
}
